﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.ComponentModel;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

#endregion

namespace ZO.SmartCore.My4CSharp
{
    /// <summary>Provides access to the information about the current user.</summary>
    /// <filterpriority>1</filterpriority>
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)]
    public class User
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>Gets or sets the current principal (for role-based security).</summary>
        /// <returns>A <see cref="T:System.Security.Principal.IPrincipal"></see> value representing the security context.</returns>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlPrincipal" /></PermissionSet>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public IPrincipal CurrentPrincipal
        {
            get
            {
                return this.InternalPrincipal;
            }
            set
            {
                this.InternalPrincipal = value;
            }
        }

        /// <summary>Gets or sets the principal object representing the current user.</summary>
        /// <returns>An <see cref="T:System.Security.Principal.IPrincipal"></see> object representing the current user.</returns>
        protected virtual IPrincipal InternalPrincipal
        {
            get
            {
                return Thread.CurrentPrincipal;
            }
            set
            {
                Thread.CurrentPrincipal = value;
            }
        }

        /// <summary>Gets a value that indicates whether the user has been authenticated.</summary>
        /// <returns>True if the user was authenticated; otherwise, False.</returns>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlPrincipal" /></PermissionSet>
        public bool IsAuthenticated
        {
            get
            {
                return this.InternalPrincipal.Identity.IsAuthenticated;
            }
        }

        /// <summary>Gets the name of the current user. </summary>
        /// <returns>String. The name of the current user.</returns>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlPrincipal" /></PermissionSet>
        public string Name
        {
            get
            {
                return this.InternalPrincipal.Identity.Name;
            }
        }
 

        #endregion

        #region Methods
        /// <summary>Sets the thread's current principal to the Windows user that started the application.</summary>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public void InitializeWithWindowsUser()
        {
            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        }

        /// <summary>Determines whether the current user belongs to the specified role. </summary>
        /// <returns>True if the current user is a member of the specified role; otherwise, False.</returns>
        /// <param name="role">String or BuiltInRole Enumeration. The role for which to check membership. </param>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlPrincipal" /></PermissionSet>
        public bool IsInRole(BuiltInRole role)
        {
            ValidateBuiltInRoleEnumValue(role, "role");
            TypeConverter converter1 = TypeDescriptor.GetConverter(typeof(BuiltInRole));
            if (this.IsWindowsPrincipal())
            {
                WindowsBuiltInRole role1 = (WindowsBuiltInRole)converter1.ConvertTo(role, typeof(WindowsBuiltInRole));
                return ((WindowsPrincipal)this.InternalPrincipal).IsInRole(role1);
            }
            return this.InternalPrincipal.IsInRole(converter1.ConvertToString(role));
        }


        /// <summary>Determines whether the current user belongs to the specified role. </summary>
        /// <returns>True if the current user is a member of the specified role; otherwise, False.</returns>
        /// <param name="role">String or BuiltInRole Enumeration. The role for which to check membership. </param>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" PathDiscovery="*AllFiles*" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlPrincipal" /></PermissionSet>
        public bool IsInRole(string role)
        {
            return this.InternalPrincipal.IsInRole(role);
        }


        private bool IsWindowsPrincipal()
        {
            return (this.InternalPrincipal is WindowsPrincipal);
        }


        internal static void ValidateBuiltInRoleEnumValue(BuiltInRole testMe, string parameterName)
        {
            if (((((((((testMe != BuiltInRole.AccountOperator) && (testMe != BuiltInRole.Administrator)) && (testMe != BuiltInRole.BackupOperator)) && (testMe != BuiltInRole.Guest)) && (testMe != BuiltInRole.PowerUser)) && (testMe != BuiltInRole.PrintOperator)) && (testMe != BuiltInRole.Replicator)) && (testMe != BuiltInRole.SystemOperator)) && (testMe != BuiltInRole.User))
            {
                throw new InvalidEnumArgumentException(parameterName, (int)testMe, typeof(BuiltInRole));
            }
        }

 


        #endregion
    }
}
